#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class PunishRcdProvider : CY.GFive.Core.Providers.DALProvider.IPunishRcdProvider
	{
		#region Sql
		private static readonly string SqlSelectPunishRcd = "SELECT [Id], [StdCode],[PnshCate],[PnshContent],[PnshDate],[PnshState],[PnshInCharge],[PnshLastDate],[PnshStopDate] FROM [PunishRcd] WHERE  [Id]=@Id";

		private static readonly string SqlInsertPunishRcd = "INSERT INTO [PunishRcd]([StdCode],[PnshCate],[PnshContent],[PnshDate],[PnshState],[PnshInCharge],[PnshLastDate],[PnshStopDate]) values(@StdCode,@PnshCate,@PnshContent,@PnshDate,@PnshState,@PnshInCharge,@PnshLastDate,@PnshStopDate);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdatePunishRcd = "UPDATE [PunishRcd] SET [StdCode] = @StdCode,[PnshCate] = @PnshCate,[PnshContent] = @PnshContent,[PnshDate] = @PnshDate,[PnshState] = @PnshState,[PnshInCharge] = @PnshInCharge,[PnshLastDate] = @PnshLastDate,[PnshStopDate] = @PnshStopDate WHERE  [Id]=@Id";

		private static readonly string SqlDeletePunishRcd = "DELETE [PunishRcd] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllPunishRcd = "SELECT [Id], [StdCode],[PnshCate],[PnshContent],[PnshDate],[PnshState],[PnshInCharge],[PnshLastDate],[PnshStopDate] FROM [PunishRcd]";
		#endregion

		#region IPunishRcdProvider Members
		public Core.Business.PunishRcd Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectPunishRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.PunishRcd punishRcd = new Core.Business.PunishRcd();

				if (!reader.IsDBNull(0)) punishRcd.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) punishRcd.StdCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) punishRcd.PnshCate = reader.GetString(2);
				if (!reader.IsDBNull(3)) punishRcd.PnshContent = reader.GetString(3);
				if (!reader.IsDBNull(4)) punishRcd.PnshDate = reader.GetDateTime(4);
				if (!reader.IsDBNull(5)) punishRcd.PnshState = reader.GetString(5);
				if (!reader.IsDBNull(6)) punishRcd.PnshInCharge = reader.GetString(6);
				if (!reader.IsDBNull(7)) punishRcd.PnshLastDate = reader.GetDecimal(7);
				if (!reader.IsDBNull(8)) punishRcd.PnshStopDate = reader.GetDateTime(8);

				return punishRcd;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.PunishRcd punishRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, punishRcd.StdCode);
			sql.AddParameter("@PnshCate", SqlDbType.NVarChar, punishRcd.PnshCate);
			sql.AddParameter("@PnshContent", SqlDbType.NVarChar, punishRcd.PnshContent);
			sql.AddParameter("@PnshDate", SqlDbType.DateTime, punishRcd.PnshDate);
			sql.AddParameter("@PnshState", SqlDbType.NVarChar, punishRcd.PnshState);
			sql.AddParameter("@PnshInCharge", SqlDbType.NVarChar, punishRcd.PnshInCharge);
			sql.AddParameter("@PnshLastDate", SqlDbType.Decimal, punishRcd.PnshLastDate);
			sql.AddParameter("@PnshStopDate", SqlDbType.DateTime, punishRcd.PnshStopDate);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertPunishRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) punishRcd.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.PunishRcd punishRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, punishRcd.StdCode);
			sql.AddParameter("@PnshCate", SqlDbType.NVarChar, punishRcd.PnshCate);
			sql.AddParameter("@PnshContent", SqlDbType.NVarChar, punishRcd.PnshContent);
			sql.AddParameter("@PnshDate", SqlDbType.DateTime, punishRcd.PnshDate);
			sql.AddParameter("@PnshState", SqlDbType.NVarChar, punishRcd.PnshState);
			sql.AddParameter("@PnshInCharge", SqlDbType.NVarChar, punishRcd.PnshInCharge);
			sql.AddParameter("@PnshLastDate", SqlDbType.Decimal, punishRcd.PnshLastDate);
			sql.AddParameter("@PnshStopDate", SqlDbType.DateTime, punishRcd.PnshStopDate);
			sql.AddParameter("@Id", SqlDbType.Int, punishRcd.Id);
			sql.ExecuteSql(SqlUpdatePunishRcd);
		}

		public void Delete(Core.Business.PunishRcd punishRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, punishRcd.Id);
			sql.ExecuteSql(SqlDeletePunishRcd);
		}

		public IList<Core.Business.PunishRcd> GetAllPunishRcd()
		{
			IList<Core.Business.PunishRcd> punishRcdlist = new List<Core.Business.PunishRcd>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllPunishRcd);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.PunishRcd punishRcd = new Core.Business.PunishRcd();

					if (!reader.IsDBNull(0)) punishRcd.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) punishRcd.StdCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) punishRcd.PnshCate = reader.GetString(2);
					if (!reader.IsDBNull(3)) punishRcd.PnshContent = reader.GetString(3);
					if (!reader.IsDBNull(4)) punishRcd.PnshDate = reader.GetDateTime(4);
					if (!reader.IsDBNull(5)) punishRcd.PnshState = reader.GetString(5);
					if (!reader.IsDBNull(6)) punishRcd.PnshInCharge = reader.GetString(6);
					if (!reader.IsDBNull(7)) punishRcd.PnshLastDate = reader.GetDecimal(7);
					if (!reader.IsDBNull(8)) punishRcd.PnshStopDate = reader.GetDateTime(8);

					punishRcd.MarkOld();
					punishRcdlist.Add(punishRcd);
				}
				reader.Close();
			}
			return punishRcdlist;
		}
		#endregion
	}
}
